package com.empire.module.member.controller.admin.internetaccount;

import com.empire.module.member.dal.dataobject.accounttype.AccountTypeDO;
import com.empire.module.member.service.accounttype.AccountTypeService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.internetaccount.vo.*;
import com.empire.module.member.dal.dataobject.internetaccount.InternetAccountDO;
import com.empire.module.member.service.internetaccount.InternetAccountService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 互联网账号")
@RestController
@RequestMapping("/member/internet-account")
@Validated
public class InternetAccountController {

    @Resource
    private InternetAccountService internetAccountService;
    @Resource
    private AccountTypeService accountTypeService;

    /**
     * 相似账号分析
     */
    @PostMapping("/analyze-similar")
    @Operation(summary = "相似账号分析")
    @PreAuthorize("@ss.hasPermission('member:internet-account:analyze')")
    public CommonResult<SimilarAccountAnalysisRespVO> analyzeSimilarAccounts(
            @Valid @RequestBody SimilarAccountAnalysisReqVO reqVO) {
        return success(internetAccountService.analyzeSimilarAccounts(reqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建互联网账号")
    @PreAuthorize("@ss.hasPermission('member:internet-account:create')")
    public CommonResult<Long> createInternetAccount(@Valid @RequestBody InternetAccountSaveReqVO createReqVO) {
        return success(internetAccountService.createInternetAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新互联网账号")
    @PreAuthorize("@ss.hasPermission('member:internet-account:update')")
    public CommonResult<Boolean> updateInternetAccount(@Valid @RequestBody InternetAccountSaveReqVO updateReqVO) {
        internetAccountService.updateInternetAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除互联网账号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:internet-account:delete')")
    public CommonResult<Boolean> deleteInternetAccount(@RequestParam("id") Long id) {
        internetAccountService.deleteInternetAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得互联网账号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:internet-account:query')")
    public CommonResult<InternetAccountRespVO> getInternetAccount(@RequestParam("id") Long id) {
        InternetAccountDO internetAccount = internetAccountService.getInternetAccount(id);
        return success(BeanUtils.toBean(internetAccount, InternetAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得互联网账号分页")
//    @PreAuthorize("@ss.hasPermission('member:internet-account:query')")
    public CommonResult<PageResult<InternetAccountRespVO>> getInternetAccountPage(@Valid InternetAccountPageReqVO pageReqVO) {
        PageResult<InternetAccountDO> pageResult = internetAccountService.getInternetAccountPage(pageReqVO);
        PageResult<InternetAccountRespVO> pageRes = BeanUtils.toBean(pageResult, InternetAccountRespVO.class);
        List<InternetAccountRespVO> list = pageRes.getList();
        // 补充账号类型
        for (InternetAccountRespVO internetAccountRespVO : list) {
            AccountTypeDO accountType = accountTypeService.getAccountType(internetAccountRespVO.getAccountTypeId());
            if (accountType != null) {
                internetAccountRespVO.setAccountTypeName(accountType.getName());
            }
        }
        return success(pageRes);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出互联网账号 Excel")
    @PreAuthorize("@ss.hasPermission('member:internet-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInternetAccountExcel(@Valid InternetAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InternetAccountDO> list = internetAccountService.getInternetAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "互联网账号.xls", "数据", InternetAccountRespVO.class,
                        BeanUtils.toBean(list, InternetAccountRespVO.class));
    }

}